﻿@model List<DeviceTypeCategory>
@{
    ViewData["Title"] = "设备类型管理";
}

 
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-inline">
                                <select name="category">
                                    <option></option>
                                    @foreach (var item in Model)
                                    {
                                        <option value="@item.Id">@item.Name</option>
                                    }
                                </select>                                
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit="" id="searchBtn" lay-filter="search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                            </button>
                            <button onclick="add()" class="layui-btn">新增设备类型</button>
                        </div>
                    </div>
                </div>

                <div class="layui-card-body">
                    
                    <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
                </div>
            </div>
        </div>

 

<script>


    //分模块加载js
    var $ = layui.$
        , view = layui.view
        , table = layui.table
        ,layer = layui.layer
        , form = layui.form,
        element = layui.element;


    form.render('select');

    //设置菜单
    $('#m_deviceType').addClass("layui-this");
    $('#m_system').addClass("layui-nav-itemed");


    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });


    //加载table
    var tableObj = table.render({
        elem: '#LAY-app-content-list'
        , url: '/deviceType/list' 
        , page: true
        , height: 'full-110'
        , cols: [[
            { field: 'title', title: '设备类型', minWidth: 300, templet: '<div>{{ d.name}}</div>' }
            , { field: 'title', title: '所属分类', minWidth: 300, templet: '<div>{{ getType(d.deviceTypeCategory)}}</div>' }
            , { field: 'sortOrder', title: '排序', width: 100, templet: '<div>{{ d.sortOrder}}</div>' }
            , {
                field: 'opr', title: '操作',width:150,
                templet: '<div class="layui-table-cell laytable-cell-11-0-8"> <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="edit(\'{{ d.id }}\')"><i class="layui-icon layui-icon-edit"></i>编辑</a>  <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\'{{ d.id }}\')"><i class="layui-icon layui-icon-delete"></i>删除</a> </div>'
            }

            ]]
            , skin: 'line'
        });

    //新增
    function add() {
        $.get("/deviceType/create", function (str) {
            var index = layer.open({
                type: 1,
                title:'新增设备类型',
                content: str,
                area:["400px","300px"]
            });

            form.render("", 'createForm');

            //监听提交
            form.on('submit(create)', function (data) {
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceType/doCreate", field, function (d) {
                    layer.msg(d.msg); if (d.code == 1) {
                        $("#searchBtn").click();
                        layer.close(index)
                    }
                });

            });
        });
    }

    //编辑
    function edit(id) {
        $.get("/deviceType/edit?id="+id, {}, function (str) {
            var index = layer.open({
                type: 1,
                content: str,
                area:["400px", "300px"]
            });

            form.render("", 'editForm');

            //监听提交
            form.on('submit(edit)', function (data) {
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceType/doEdit", field, function (d) {
                    layer.msg(d.msg); if (d.code == 1) {
                        $("#searchBtn").click();
                        layer.close(index)
                    }
                });

            });
        });
    }

    function deleteItem(id) {
        layer.confirm("确定要删除吗？", function (index) {
            layer.close(index);
            $.post("/deviceType/delete?id=" + id, function (d) {
                layer.msg(d.msg);
                if (d.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }


    function getType(type) {
        if (type != null) return type.name;
        return "";
    }


    $(document).on('keydown', function (e) {

        if (e.keyCode == 13) {
            $("#searchBtn").click();
        }
    })
</script>

